require("NodeManager")
HeroFunc={}
Hero={}
Hero.HeroState = {NoDef=0, Normal=1, Fire=2, Hurt=3, SkillFont=4, Skill=5, Hurted=6}
Hero.View2HeroMap = {}

HeroFunc.__index=HeroFunc
setmetatable(Hero, HeroFunc)

HeroFunc.release = function(self)
  print("HeroFunc.release", self)
  Hero.View2HeroMap[tostring(self.normalView)] = nil
  self.parent:removeChild(self.normalView, true)
  self.parent:removeChild(self.hpView, true)
  self.parent:removeChild(self.spView, true)
  self.parent:removeChild(self.bkHpView, true)
  self.parent:removeChild(self.skillView, true)
end
HeroFunc.getArmyConfig = function(self, armyid)
  for k,v in pairs(UserData.config.models.Soldier) do
    if v.id==armyid then
      return v
    end
  end
end
HeroFunc.create = function(self, parent, gid, sid, position, callback, target)
  print("HeroFunc.create", gid, sid)
  hero={}
  setmetatable(hero, HeroFunc)
  hero.callback = callback
  hero.target = target 
  --hero.soldierName = soldierName
  hero.parent=parent 
  hero.data = {}
  hero.currentState = Hero.HeroState.NoDef
  hero.oldState = Hero.HeroState.NoDef
  
  local armyId = sid 
  local armyConfig = hero:getArmyConfig(armyId)
  --hero.soldierName = armyConfig.asset
  hero.soldierName = "qingtan" 
 
  if gid~=0 then 
    local general = General:create(gid)
    --local armyId = general.generalConfig.armyId 
    local skillId = general.generalConfig.skill
    local skillConfig = UserData.config.models.NewSkill[skillId] 
    --hero.skillName = skillConfig.attackEffect
    --hero.skillFontName = skillConfig.fontAsset 
    hero.skillName = "zuoyoukaigong"--"tiebi" 
    hero.skillFontName = "zuoyoukaigong"--"tiebi" 
  end
  --for k,v in pairs(HeroData[soldierName]) do
  --  hero.data[k] = v
  --end
 
  print("HeroFunc.create", hero, parent, hero.soldierName, hero.skillName, hero.skillFontName) 
  CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(string.format("%s.plist", hero.soldierName))
  local sp = CCSprite:createWithSpriteFrameName(string.format("%s_idle_0001.png", hero.soldierName))
  sp:setPosition(position)
  --sp:setScale(1.5)
  --sp:setAnchorPoint(ccp(0,0))
  --sp:setFlipX(flip)
  parent:addChild(sp)

  --local ac = ActionUtil.createAction(string.format("%s.plist", hero.soldierName), 0.2, string.format("%s_idle", hero.soldierName))
  --sp:runAction(CCRepeatForever:create(ac))
  
  --[[
  local bksp = CCSprite:create("war_hp_bg.png")
  local hpsp = CCSprite:create("war_hp.png")
  local spsp = CCSprite:create("war_sp.png")
  local hpbar = CCProgressTimer:create(hpsp)
  local spbar = CCProgressTimer:create(spsp)
  hpbar:setType(kCCProgressTimerTypeBar)
  hpbar:setPercentage(100.0)
  hpbar:setMidpoint(ccp(0,0))
  hpbar:setBarChangeRate(ccp(1, 0))
  spbar:setType(kCCProgressTimerTypeBar)
  spbar:setPercentage(100.0)
  spbar:setMidpoint(ccp(0,0))
  spbar:setBarChangeRate(ccp(1, 0))

  parent:addChild(bksp)
  parent:addChild(hpbar)
  parent:addChild(spbar)
  bksp:setPosition(position.x,position.y+80)
  hpbar:setPosition(position.x,position.y+80+5)
  spbar:setPosition(position.x,position.y+80-5)
  ]]
  --NodeManager:addObject("hpbar", hpbar)
  --NodeManager:addObject("spbar", hpbar)

  --NodeManager:addObject(string.format("%s-normal", hero.soldierName), sp)
  --NodeManager:addObject(string.format("%s-hp", hero.soldierName), hpbar)
  --NodeManager:addObject(string.format("%s-sp", hero.soldierName), spbar)
  hero.normalView = sp 
  --hero.hpView = hpbar
  --hero.spView = spbar
  --hero.bkHpView = bksp 
  --NodeManager:addObject(hero.soldierName, hero)
  print("create", tostring(hero.normalView), hero)
  Hero.View2HeroMap[tostring(hero.normalView)] = hero 
  hero:setHeroState(Hero.HeroState.Normal)
  return hero
end


function onRemoveFromParent(node)
  node:getParent():removeChild(node, true)
end

HeroFunc.showHurtNumber = function(self)
  local label = CCLabelTTF:create(string.format("-%d", self.hurt), "Arial", 32)
  label:setColor(ccc3(255,0,0))
  local px, py = self.normalView:getPosition()
  label:setPosition(ccp(px, py+20))
  self.normalView:getParent():addChild(label)
  local mac = CCMoveBy:create(0.3, ccp(0, 60))
  local sac = CCScaleBy:create(0.3, 1.5)
  label:runAction(mac)
  label:runAction(ActionUtil.createSeqAction({sac, CCDelayTime:create(0.2), CCFadeOut:create(0.2), CCCallFuncN:create(onRemoveFromParent)}))
end

HeroFunc.setHeroState = function(self, newState)
  self.normalView:stopAllActions()
  if self.currentState == newState then return nil; end
  if newState==Hero.HeroState.Normal then 
    local ac = ActionUtil.createAction(string.format("%s.plist", self.soldierName), 0.1, string.format("%s_idle", self.soldierName))
    self.normalView:runAction(CCRepeatForever:create(ac))
  elseif newState==Hero.HeroState.Fire then 
    local ac = ActionUtil.createAction(string.format("%s.plist", self.soldierName), 0.1, string.format("%s_fire", self.soldierName))
    self.normalView:runAction(ActionUtil.createSeqAction({ac, CCDelayTime:create(0.5), CCCallFuncN:create(onHeroAction)}))
  elseif newState==Hero.HeroState.Hurt then 
    local ac = ActionUtil.createAction(string.format("%s.plist", self.soldierName), 0.02, string.format("%s_damage", self.soldierName))
    self.normalView:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onHeroAction)}))
    --self.normalView:runAction(ActionUtil.createSeqAction({ac, CCDelayTime:create(0.5), CCCallFuncN:create(onHeroAction)}))
    --self.normalView:runAction(ActionUtil.createSeqAction({CCDelayTime:create(0.5), CCCallFuncN:create(onHeroAction)}))
    --self:showHurtNumber()
  elseif newState==Hero.HeroState.Hurted then
    self:showHurtNumber()
    self.normalView:runAction(ActionUtil.createSeqAction({CCDelayTime:create(0.5), CCCallFuncN:create(onHeroAction)}))
  elseif newState==Hero.HeroState.SkillFont then  
  elseif newState==Hero.HeroState.Skill then
    if self.skillFontName~=nil then
      print("HeroFunc show skill font", self.skillFontName)
      local sp = CCSprite:create(string.format("FONT_%s.png", self.skillFontName))
      sp:setPosition(ccp(480, 400))
      --sp:setScale(0.1)
      self.normalView:getParent():addChild(sp)
      local sac = CCScaleBy:create(0.5, 2)
      sp:runAction(ActionUtil.createSeqAction({sac, CCDelayTime:create(0.5), CCFadeOut:create(0.3), CCCallFuncN:create(onRemoveFromParent)})) 
    end
    if self.skillName~=nil then
      local ac = ActionUtil.createAction(string.format("skill_attack_%s.plist", self.skillName), 0.1, "")
      local sp = CCSprite:createWithSpriteFrameName(string.format("skill_attack_%s_0.png", self.skillName, self.skillName))
      sp:setPosition(self.normalView:getPosition())
      self.normalView:getParent():addChild(sp)
      sp:runAction(CCRepeatForever:create(ac)) 
      self.skillView = sp
      sp:runAction(ActionUtil.createSeqAction({ac, CCCallFuncN:create(onHeroAction)}))
      
      Hero.View2HeroMap[tostring(sp)] = self 
    else
      self.normalView:runAction(ActionUtil.createSeqAction({CCDelayTime:create(0.5), CCCallFuncN:create(onHeroAction)}))
    end
  end
  self.oldState = self.currentState
  self.currentState = newState
  print("HeroFunc.setHeroState", self, self.oldState, newState)
end

function onHeroAction(node)
  print("onHeroAction", tostring(node))
  local hero = Hero.View2HeroMap[tostring(node)]
  --hero.onHeroStateChanged(hero) 
  if hero.currentState==Hero.HeroState.Skill and hero.skillView~=nil then
    hero.skillView:getParent():removeChild(hero.skillView, true)
    hero.skillView = nil
  end 
  local metaFunc = getmetatable(hero.target)
  metaFunc[hero.callback](hero.target, hero)
end
